package com.have.blog.picture.utils;

import com.have.blog.base.enums.BlogStatus;
import com.have.blog.model.constant.SQLConstant;
import com.have.blog.model.mapper.TSystemConfigMapper;
import com.have.blog.model.models.TSystemConfig;
import com.have.blog.model.models.TSystemConfigExample;
import com.have.blog.xo.service.cache.SystemConfigCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Component
@Slf4j
@SuppressWarnings("all")
public class FeignUtil {
    @Autowired
    private SystemConfigCacheService systemConfigCacheService;
    @Autowired
    private TSystemConfigMapper systemConfigMapper;
    /**
     * 从token中获取系统配置
     * @return
     */
    public TSystemConfig getSystemConfig() {
        // TODO 根据token获取系统配置?
        TSystemConfig systemConfig = systemConfigCacheService.getSystemConfig();
        if (systemConfig == null) {
            TSystemConfigExample example = new TSystemConfigExample();
            example.createCriteria()
                    .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
            example.setOrderByClause(SQLConstant.CREATE_TIME);
            List<TSystemConfig> configs = systemConfigMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(configs)) {
                return null;
            }
            systemConfig = configs.get(0);
        }
        return systemConfig;
    }
}
